package blackstar.agent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import blackstar.agent.event.AfterStartupEvent;
import blackstar.agent.event.EventListener;
import blackstar.agent.event.EventMediator;
import blackstar.agent.event.StartupEvent;

/**
 * @author raywu (ayufox@gmail.com)
 * 
 */
public class AgentMain
{
	private final static Log LOGGER = LogFactory.getLog(AgentMain.class);

	public static void main(String[] args) throws Exception
	{
		Thread.currentThread().setContextClassLoader(AgentMain.class.getClassLoader());
		
		String listeners = AgentConfig.getProperty("listeners");
		String[] listenerArray = listeners.split(",");
		for (String listener : listenerArray)
		{
			final EventListener eventListener = (EventListener) Class.forName(
					listener.trim()).newInstance();
			EventMediator.registerListener(eventListener);
			LOGGER.info("Regist EventListner[" + listener + "]");
		}
		EventMediator.registerListener(new ShutdownHook());

		LOGGER.info("Dispatch StartupEvent");
		EventMediator.dispatchEvent(new StartupEvent());
		LOGGER.info("Dispatch AfterStartupEvent");
		EventMediator.dispatchEvent(new AfterStartupEvent());
		LOGGER.info("Startup Success");
	}
}